package com.dola.web.security;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Component
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {

	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException exception) throws IOException {
		response.setStatus(500);
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		StringBuilder builder = new StringBuilder();
		builder.append("{");
		builder.append("\"status\":" + 500);
		builder.append(",\"code\":" + "0001");
		builder.append(",\"errorMes\":" + exception.getMessage());
		builder.append(",\"path\": ");
		builder.append(",\"timestamp\":" + new Date().toString());

		builder.append("}");
		writer.print(builder.toString());
	}

}
